package dao

import (
	"fmt"
	"go_cms/app/param"
	"go_cms/utils"
	"strings"
)

type SystemDao struct {
	*utils.Db
}

// 获取用户列表信息
func (this *SystemDao)UsersList(queryInfo param.QueryUserInfo) ([]*param.Users, int64){
	var users []*param.Users
	this.Table("users").Where("name LIKE ? and cellphone LIKE ?",
		"%"+queryInfo.Name+"%","%"+queryInfo.Cellphone+"%").Limit(queryInfo.Size).Offset(queryInfo.Offset).Find(&users)
	//this.Table("users").Find(&users)
	if utils.Debug{
		fmt.Printf("systemdao.go 18 users= %#v",users)
	}
	var count int64
	this.Table("users").Count(&count)
	return users,count
}


// 获取角色列表信息
func (this *SystemDao)RoleList() ([]*param.Role, int64){
	var role []*param.Role
	this.Table("roles").Find(&role)
	if utils.Debug{
		fmt.Printf("systemdao.go 30 roles= %#v",role[0])
	}
	var count int64
	this.Table("roles").Count(&count)
	//var menu []*param.Menu
	for _,v:= range role{
		var menu []*param.Menu
		menu_list := strings.Split(v.MenuList,",")
		this.Table("menus").Where("id IN ?",menu_list).Find(&menu)
		v.MenusList = utils.MakeMenuTree(menu)
	}

	return role,count
}

// 获取角色列表信息
func (this *SystemDao)MenuList() ([]*param.Menu, int64){
	var menus []*param.Menu
	this.Table("menus").Find(&menus)
	if utils.Debug{
		fmt.Printf("systemdao.go 45 menus= %#v",menus[0])
	}
	var count int64
	this.Table("menus").Count(&count)
	return menus,count
}

// 获取部门列表信息
func (this *SystemDao)DepartmentList(queryInfo param.QueryUserInfo) ([]*param.Department, int64){
	var departments []*param.Department
	this.Table("departments").Limit(queryInfo.Size).Offset(queryInfo.Offset).Find(&departments)
	if utils.Debug{
		fmt.Printf("systemdao.go 66 departments= %#v",departments[0])
	}
	var count int64
	this.Table("departments").Count(&count)
	return departments,count
}

// 创建用户
func (this *SystemDao)CreateUser(param param.Users) {
	res:=this.Table("users").Create(&param)
	if utils.Debug{
		fmt.Printf("systemdao.go 70 CreateUser= %#v",res)
	}

}

// 更新用户
func (this *SystemDao)UpdateUser(uid string,  params param.Users) {

	this.Table("users").Where("id = ?", uid).Updates(&params)

}

// 创建用户
func (this *SystemDao)CreateRole(param param.Role) {
	res:=this.Table("roles").Create(&param)
	if utils.Debug{
		fmt.Printf("systemdao.go 70 CreateUser= %#v",res)
	}

}

// 更新角色权限
func (this *SystemDao)UpdateRole(uid string,  params param.Role) {

	this.Table("roles").Where("id = ?", uid).Updates(&params)

}